#include "shell.h"

int main(int argc, char *argv[]){
	currentPath = malloc(sizeof(char) * PATH_LENGTH);
	homeDir = malloc(sizeof(char) * PATH_LENGTH);
	workingPath = NULL;
	// workingPath->pathVal = NULL;
	// workingPath->next = NULL;
	int res;
	char *line;
	t_CmdLine *CmdLine;


	init();
	res = 0;
	getcwd(currentPath, PATH_LENGTH);
	getcwd(homeDir, PATH_LENGTH);
	while (res != 1) {
		prompt();
		CmdLine = malloc(sizeof(t_CmdLine));
		line = malloc(sizeof(char) * 4100);
		readCmdLine(line);
		if (parse(CmdLine, line) == -1)
		{
			printf("Syntax error\n");
			free(CmdLine);
			free(line);
			continue;
		}

		//test
/*		t_args *arg_test;
		t_file *file_test;
		while (CmdLine)
		{
			printf("[cmd: %s]\n", CmdLine->cmd->cmd);
			arg_test = CmdLine->cmd->arguments;
			while (arg_test)
			{
				printf("[arg: %s]\n", arg_test->arg);
				arg_test = arg_test->next;
			}
			file_test = CmdLine->cmd->inFile;
			while (file_test)
			{
				printf("[inFile: %s]\n", file_test->fileName);
				file_test = file_test->next;
			}
			file_test = CmdLine->cmd->outFile;
			while (file_test)
			{
				printf("[outFile: %s]\n", file_test->fileName);
				file_test = file_test->next;
			}
			file_test = CmdLine->cmd->errMsg;
			while (file_test)
			{
				printf("[errMsg: %s]\n", file_test->fileName);
				file_test = file_test->next;
			}
			CmdLine = CmdLine->next;
		}
*/
//		history();
		res = execute(CmdLine);
		free(CmdLine);
		free(line);
	}
	return 0;
}

void init()
{
	return;
}

void prompt()
{
	printf("@ %s @ ", currentPath);
	return;
}

void readCmdLine(char *line)
{
	char c;
	int i;

	if (line == NULL)
		return;

	c = getchar();
	i = 0;
	while (c != '\n' && i < MAX_CMD_LEN)
	{
		line[i] = c;
		i++;
		c = getchar();
	}
	line[i] = '\0';
	return;
}
